package com.meevle.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.red5.logging.Red5LoggerFactory;
import org.slf4j.Logger;

import com.meevle.business.PersistenceManagerImpl;
import com.meevle.vo.SetVO;
import com.meevle.vo.SlotVO;
import com.meevle.vo.UserVO;

@Entity
public class UserDAO implements Serializable {

	private static final long serialVersionUID = 1L;

	private static Logger log = Red5LoggerFactory.getLogger(UserDAO.class,
			"Meevle");

	private long id;
	private String username;
	private String password;

	private List<SetDAO> projects = new ArrayList<SetDAO>();

	private List<SlotDAO> tracks = new ArrayList<SlotDAO>();
	
	private List<SlotCommentDAO> comments = new ArrayList<SlotCommentDAO>();

	public UserDAO() {
	}

	public UserDAO(String username, String password) {
		setUsername(username);
		setPassword(password);
	}

	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

	private void setId(long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@OneToMany(cascade = {CascadeType.MERGE, CascadeType.REFRESH }, mappedBy = "owner")
	public List<SetDAO> getSets() {
		return projects;
	}

	public void setSets(List<SetDAO> projects) {
		this.projects = projects;
	}

	@OneToMany(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, mappedBy = "owner")
	public List<SlotDAO> getSlots() {
		return tracks;
	}

	public void setSlots(List<SlotDAO> tracks) {
		this.tracks = tracks;
	}
	
	@OneToMany(cascade={CascadeType.MERGE, CascadeType.REFRESH}, mappedBy="author")
	public List<SlotCommentDAO> getComments() {
		return comments;
	}
	
	public void setComments(List<SlotCommentDAO> comments) {
		this.comments = comments;
	}

	/**
	 * inserts a user in the backing database
	 * 
	 * @param user
	 *            the user that will be added to the database
	 * @throws Exception
	 *             if during the adding to the DB the inserting failed
	 */
	public static void persist(UserDAO user) throws Exception {
		PersistenceManagerImpl.persist(user);
	}

	/**
	 * Gets a user from the Database with that specified username
	 * 
	 * @param user The user you want to get from the database
	 * @return a com.meevle.dao.UserDAO object
	 * @throws IllegalArgumentException if there's nog user in the database with that username
	 * @throws Exception If something went wrond suring the DB access
	 */
	public static UserDAO getUser(UserVO user)throws IllegalArgumentException, Exception
	{
		UserDAO u = null;
		try {
			u = PersistenceManagerImpl.getUser(user);
		} catch (Exception e) {
			throw e;
		}
		if (u == null)
			throw new IllegalArgumentException("The user " + user
					+ " does not exist.");

		return u;
	}

	/**
	 * Returns the projects of a user
	 * 
	 * @param userVo
	 *            the user we want the projects from
	 * @return a list of projects
	 * @throws IllegalArgumentException
	 *             if the user doesn't exist in the DB
	 * @throws Exception If something went wrong with the DB access
	 */
	public static List<SetDAO> getSetsByUser(UserVO userVo)throws IllegalArgumentException, Exception
	{
		List<SetDAO> projectDaos;

		UserDAO u;
		try {
			u = getUser(userVo);
		} catch (IllegalArgumentException e) {
			throw e;
		} catch (Exception e) {
			throw e;
		}
		projectDaos = u.getSets();

		return projectDaos;
	}

	/**
	 * Get the tracks of a user
	 * 
	 * @param a
	 *            com.meevle.vo.UserVO object you would like to have the tracks
	 *            from
	 * @return a <code>List</code> of tracks
	 * @throws IllegalArgumentException
	 *             if the user doesn't exist in the DB
	 * @throws Exception If something went wrong during the accessing of the DB
	 */
	public static List<SlotDAO> getSlotsByUser(UserVO userVo) throws IllegalArgumentException, Exception
	{
		List<SlotDAO> trackDaos = new ArrayList<SlotDAO>();

		UserDAO user;
		try {
			user = getUser(userVo);
		} catch (IllegalArgumentException e) {
			throw e;
		} catch (Exception e) {
			throw e;
		}
		trackDaos = user.getSlots();

		return trackDaos;
	}

	/**
	 * Get all the users from the database
	 * 
	 * @return a <code>List</code> of <code>UserDAO</code> objects
	 */
	public static List<UserDAO> getAll()throws Exception
	{
		List<UserDAO> ret = new ArrayList<UserDAO>();
		try {
			ret = PersistenceManagerImpl.getAllUsers();
		} catch (Exception e) {
			throw e;
		}
		return ret;
	}
	
	/**
	 * Update a user
	 * @param user The user you'd like to update
	 * @throws Exception If there was a problem with the update
	 */
	public static void update(UserVO oldVo, UserVO newVo)throws Exception
	{
			try {
				PersistenceManagerImpl.updateUser(oldVo, newVo);
				oldVo.setUsername(newVo.getUsername());
			} catch (Exception e) {
				e.printStackTrace();
				throw e;
			}
	}
	
	public static void addSetToUser(UserVO userVo, SetVO projectVo)
	{
		PersistenceManagerImpl.addSetToUser(userVo, projectVo);	
	}
	
	public static void addSlotToUser(UserVO userVo, SlotVO trackVo)throws IllegalArgumentException, Exception
	{
		try {
			PersistenceManagerImpl.addTrackToUser(userVo, trackVo);
		} catch (IllegalArgumentException e) {
			throw e;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UserDAO other = (UserDAO) obj;
		if (id != other.id)
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
}
